#include <stdio.h>
#include <string.h>

#define MAX_LEN 100

int help();
int quit();

typedef struct tDataNode {
    char   *cmd;
    char   *desc;
    int    (*handler)();
    struct tDataNode *next;
} DataNode;

static DataNode head[] = {
    {"help", "This is menu program.", help, &head[1]},
    {.cmd = "version", .desc = "Menu program v1.0", NULL, &head[2]},
    {.cmd = "quit", .desc = "Quit menu program.", .handler = quit, .next = NULL}
}; 

int main(int argc, char *argv[])
{
    int exitFlag = 0;
    char cmd[MAX_LEN];
    while(1)
    {
        printf("Input a command: ");
        scanf("%s", cmd);
        DataNode *p = head;        
        while(NULL != p)
        {
            if(0 == strcmp(cmd, p->cmd))
            {
                printf("%-10s - %s\n", cmd, p->desc);
                if(NULL != p->handler)
                {
                    exitFlag = p->handler();
                }
                break;
            }
            p = p->next;
        }
        if(-1 == exitFlag)
        {
            break;
        }
        if(NULL == p)
        {
            printf("Unknown command!\n");
        }        
    }

    return 0;
}

int help()
{
    DataNode *p = head;        
    printf("Menu List:\n");
    while(NULL != p)
    {
        printf("%-10s - %s\n", p->cmd, p->desc);
        p = p->next;
    }
    return 0;
}

int quit()
{
    return -1;
}
